package com.mjh.zhujianju.service.party.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTEntityUtils;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.party.BuildingClassDetailMapper;
import com.mjh.zhujianju.model.party.BuildingClassDetailEO;
import com.mjh.zhujianju.model.party.BuildingClassEO;
import com.mjh.zhujianju.model.party.BuildingClassUsersDetailsEO;
import com.mjh.zhujianju.model.party.BuildingClassUsersEO;
import com.mjh.zhujianju.service.party.BuildingClassDetailService;
import com.mjh.zhujianju.service.party.BuildingClassUsersDeatilsService;
import com.mjh.zhujianju.service.party.BuildingClassUsersService;
import com.mjh.zhujianju.vo.party.BuildingClassDetailVO;

/**
 * @author liuyang
 * @Description: 课堂详情
 * @date 2018/8/15 21:46
 */
@Service
public class BuildingClassDetailServiceImpl extends BaseServiceImpl<BuildingClassDetailEO> implements BuildingClassDetailService {

    @Autowired
    private BuildingClassDetailMapper buildingClassDetailMapper;

    @Autowired
    private BuildingClassUsersService buildingClassUsersService;

    @Autowired
    private BuildingClassUsersDeatilsService buildingClassUsersDeatilsService;

    @Autowired
    private FileService fileService;

    public BuildingClassDetailServiceImpl(BuildingClassDetailMapper buildingClassDetailMapper) {
        super(buildingClassDetailMapper);
    }

    @Override
    public List<BuildingClassDetailVO> queryBuildingClassDetailTotal(List<Long> classIds) {
        return buildingClassDetailMapper.queryBuildingClassDetailTotal(classIds);
    }

    @Override
    public List<BuildingClassDetailVO> queryBuildingClassDetailByClassId(Long classId) {
        List<BuildingClassDetailVO> detailVOList = Lists.newArrayList();
        List<BuildingClassDetailEO> list = buildingClassDetailMapper.selectDetailByClassId(classId);
        if (CollectionUtils.isEmpty(list)){
            return detailVOList;
        }

        //组装
        list.stream().forEach(buildingClassDetailEO1 -> {
            BuildingClassDetailVO buildingClassDetailVO = new BuildingClassDetailVO();
            BeanUtils.copyProperties(buildingClassDetailEO1,buildingClassDetailVO);
            detailVOList.add(buildingClassDetailVO);
        });

        //查询文件
        List<Long> ids = list.stream().map(buildingClassDetailEO1 -> buildingClassDetailEO1.getId()).distinct().collect(Collectors.toList());

        List<FileEO> fileList = fileService.getFileByIds(ids,BuildingClassEO.class.getName(),null);
        if (CollectionUtils.isEmpty(fileList)){
            return detailVOList;
        }

        for (BuildingClassDetailVO buildingClassDetailVO : detailVOList){
            for (FileEO fileEO : fileList){
                if (buildingClassDetailVO.getId().equals(fileEO.getCaseId())){
                    buildingClassDetailVO.setFileId(fileEO.getId());
                    continue;
                }
            }
        }
        return detailVOList;
    }

    @Override
    public List<BuildingClassDetailEO> selectBuildingClassDetailByClassId(Long classId) {
        BuildingClassDetailEO buildingClassDetailEO = new BuildingClassDetailEO();
        buildingClassDetailEO.setClassId(classId);
        return buildingClassDetailMapper.selectListByCondition(buildingClassDetailEO);
    }

    @Override
    public Map<Long, Long> getMapTopOne(List<Long> ids) {
        Map<Long, Long> map = new HashMap<Long, Long>();
        if (XTListUtils.isEmpty(ids)) {
            return map;
        }
        List<BuildingClassDetailEO> buildingClassDetailEOS = buildingClassDetailMapper.queryBuildingClassDetail(ids);
        if(!CollectionUtils.isEmpty(buildingClassDetailEOS)){
            for(BuildingClassDetailEO buildingClassDetailEO : buildingClassDetailEOS){
                Long count = map.get(buildingClassDetailEO.getClassId());
                if(count == null){
                    map.put(buildingClassDetailEO.getClassId(), 1L);
                }else {
                    map.put(buildingClassDetailEO.getClassId(),count+1);
                }
            }

        }
        return map;
    }

    @Override
    public Long queryClassDetailFileId(Long id,Long userId) throws XTBusinessException {
        BuildingClassDetailEO eo = buildingClassDetailMapper.selectByPrimaryKey(id);
        List<FileEO> fileEOList = fileService.getFileById(id,BuildingClassDetailEO.class.getName(),null);
        if (CollectionUtils.isEmpty(fileEOList)){
            return null;
        }

        //查看当前浏览用户是否已加入学习计划中
        List<BuildingClassUsersEO> list = buildingClassUsersService.findBuildingClassUsersEOByUserId(userId);
        if (!CollectionUtils.isEmpty(list)){
            BuildingClassUsersEO buildingClassUsersEO = list.get(0);
            //简单处理，点击就是100%

            //查询课件记录
            List<BuildingClassUsersDetailsEO> buildingClassUsersDetailsEOList = buildingClassUsersDeatilsService.findBuildingClassUsersEOByUserId(buildingClassUsersEO.getId());
            //单个课件更为100%
            for (BuildingClassUsersDetailsEO buildingClassUsersDetailsEO : buildingClassUsersDetailsEOList){
                if (buildingClassUsersDetailsEO.getClassDetailId().equals(id)){
                    buildingClassUsersDetailsEO.setSchedule(100);
                    buildingClassUsersDeatilsService.updateByPrimaryKeySelective(buildingClassUsersDetailsEO);
                    break;
                }
            }

            //总课件更新进度
            int count = buildingClassUsersDetailsEOList.size();
            int detailCount = 0;
            for (BuildingClassUsersDetailsEO buildingClassUsersDetailsEO : buildingClassUsersDetailsEOList){
                detailCount = detailCount+buildingClassUsersDetailsEO.getSchedule();
            }
            buildingClassUsersEO.setSchedule(detailCount/count);
            buildingClassUsersService.updateByPrimaryKeySelective(buildingClassUsersEO);
        }
        return fileEOList.get(0).getId();
    }

    @Override
    public void saveInfo(BuildingClassDetailVO buildingClassDetailVO) throws XTBusinessException {
        BuildingClassDetailEO buildingClassDetailEO = new BuildingClassDetailEO();
        BeanUtils.copyProperties(buildingClassDetailVO, buildingClassDetailEO);
        this.save(buildingClassDetailEO);
        if (!XTListUtils.isEmpty(buildingClassDetailVO.getFiles())) {
            fileService.updateList(buildingClassDetailVO.getFiles(), buildingClassDetailEO.getId(), BuildingClassDetailEO.class.getName());
        }
    }

    @Override
    public PagedResult<BuildingClassDetailVO> findBuildingClassDetailPagination(BuildingClassDetailVO buildingClassDetailVO) {
        Integer pageNo = buildingClassDetailVO.getPageNo() == null ? 1 : buildingClassDetailVO.getPageNo();
        Integer pageSize = buildingClassDetailVO.getPageSize() == null ? 15 : buildingClassDetailVO.getPageSize();
        PageHelper.startPage(pageNo, pageSize);
        List<BuildingClassDetailEO> list = buildingClassDetailMapper.selectPagination(buildingClassDetailVO);
        PagedResult<BuildingClassDetailEO> page = PagedBeanUtil.toPagedResult(list);
        PagedResult<BuildingClassDetailVO> result = new PagedResult<BuildingClassDetailVO>();
        BeanUtils.copyProperties(page, result);
        if (!XTListUtils.isEmpty(list)) {
            List<Long> ids = XTEntityUtils.getIdList(list);
            List<BuildingClassDetailVO> voList = new ArrayList<BuildingClassDetailVO>();
            Map<Long, FileEO> files = fileService.getMapTopOne(ids, BuildingClassDetailEO.class.getName(), null);
            for (BuildingClassDetailEO buildingClassEO1 : list) {
                BuildingClassDetailVO d = new BuildingClassDetailVO();
                BeanUtils.copyProperties(buildingClassEO1, d);
                voList.add(d);
                FileEO file = files.get(d.getId());
                if (file != null) {
                    d.getFiles().add(file.getId());
                    d.getFileEOList().add(file);
                }
            }
            result.setDataList(voList);
        }
        return result;
    }

	@Override
	public Integer queryBuildingClassDetailTotalByClassId(Long classId) {
		return buildingClassDetailMapper.queryBuildingClassDetailTotalByClassId(classId);
	}


}
